package com.iflytek.cosmo.ocr.common.config;

import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
 * RestServiceConfig
 *
 */
@Configuration
public class RestServiceConfig {

	@Value("${http.restTemplate.connect-timeout}")
	private int connectTimeOut;

	@Value("${http.restTemplate.read-timeout}")
	private int readTimeOut;

	@Value("${http.restTemplate.connection-request-timeout}")
	private int connectionRequestTimeOut;

	@Value("${http.restTemplate.collection.pool.maxTotal}")
	private int maxToal;

	@Value("${http.restTemplate.collection.pool.defaultMaxPerRoute}")
	private int defaultMaxPerRoute;

	/**
	 * 让spring管理RestTemplate,参数相关配置
	 * @param builder
	 * @return
	 */
	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder) {
		RestTemplate restTemplate = builder.build();// 生成一个RestTemplate实例
		restTemplate.setRequestFactory(clientHttpRequestFactory());
		List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
		messageConverters.removeIf(converter -> converter instanceof StringHttpMessageConverter);
		messageConverters.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
		return restTemplate;
	}

	/**
	 * 客户端请求链接策略
	 *
	 * @return
	 */
	@Bean
	public ClientHttpRequestFactory clientHttpRequestFactory() {
		HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
		clientHttpRequestFactory.setHttpClient(httpClientBuilder().build());
		clientHttpRequestFactory.setConnectTimeout(connectTimeOut); // 连接超时时间/毫秒
		clientHttpRequestFactory.setReadTimeout(readTimeOut); // 读写超时时间/毫秒
		clientHttpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeOut);// 请求超时时间/毫秒
		return clientHttpRequestFactory;
	}

	/**
	 * 设置HTTP连接管理器,连接池相关配置管理
	 *
	 * @return 客户端链接管理器
	 */
	@Bean
	public HttpClientBuilder httpClientBuilder() {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		httpClientBuilder.setConnectionManager(poolingConnectionManager());
		return httpClientBuilder;
	}

	/**
	 * 链接线程池管理,可以keep-alive不断开链接请求,这样速度会更快 MaxTotal 连接池最大连接数 DefaultMaxPerRoute
	 * 每个主机的并发 ValidateAfterInactivity
	 * 可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间，如果超过，释放socket重新建立
	 *
	 * @return
	 */
	@Bean
	public HttpClientConnectionManager poolingConnectionManager() {
		PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
		poolingConnectionManager.setMaxTotal(maxToal);
		poolingConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
		return poolingConnectionManager;
	}

	public List<Map<String, Object>> customer;

	public List<Map<String, Object>> getCustomer() {
		return customer;
	}

	public void setCustomer(List<Map<String, Object>> customer) {
		this.customer = customer;
	}
}
